package edu.fib.upc.erdapfel;

import org.mapsforge.android.maps.MapView;
import org.mapsforge.android.maps.Projection;
import org.mapsforge.android.maps.overlay.Overlay;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff.Mode;

public class AreaPickerOverlay extends Overlay{

	private Point p1;
	private Point p2;
	private Paint mPaint;
	private Paint tPaint;
	public AreaPickerOverlay(){

		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.parseColor("#ffA4C639"));
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(5);
		mPaint.setPathEffect(new DashPathEffect(new float[] {10,20}, 0));		
		mPaint.setShadowLayer(mPaint.getStrokeWidth()/2, mPaint.getStrokeWidth()/4, mPaint.getStrokeWidth()/4, Color.BLACK);
		
		tPaint = new Paint();
		tPaint.setDither(true);
		tPaint.setAntiAlias(true);
		tPaint.setColor(Color.parseColor("#ffA4C639"));
		tPaint.setStyle(Paint.Style.STROKE);
		tPaint.setStrokeJoin(Paint.Join.MITER);
		tPaint.setStrokeCap(Paint.Cap.ROUND);
		tPaint.setStrokeWidth(1.5f);					
		tPaint.setTextAlign(Align.CENTER);
		tPaint.setTextSize(12);		
		tPaint.setShadowLayer(tPaint.getStrokeWidth()/2, tPaint.getStrokeWidth()/4, tPaint.getStrokeWidth()/4, Color.BLACK);
	


		p1 = new Point();
		p2 = new Point();
	}   

	public void draw(Canvas canvas, MapView mapv, boolean shadow){
		super.draw(canvas);						
		
		Path path = new Path();

		if(p1.equals(new Point()) && p2.equals(new Point())) {
			// initially the area begins in the middle
			p1.x = canvas.getWidth() / 2 - canvas.getWidth() / 4;
			p2.x = canvas.getWidth() / 2 + canvas.getWidth() / 4;
			p1.y = canvas.getHeight() / 2 - canvas.getHeight() / 4;
			p2.y = canvas.getHeight() / 2 + canvas.getHeight() / 4;
		}

		path.moveTo(p1.x, p1.y);
		path.lineTo(p2.x, p1.y);
		path.lineTo(p2.x, p2.y);
		path.lineTo(p1.x, p2.y);
		path.lineTo(p1.x, p1.y);

		canvas.drawColor(0, Mode.CLEAR);
		int xPos = (canvas.getWidth() / 2);
		int yPos = (int) ((canvas.getHeight() / 10) - ((tPaint.descent() + tPaint.ascent()) / 2)) ; //((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.				
		canvas.drawText("area picker mode", xPos , yPos, tPaint);
		canvas.drawPath(path, mPaint);
	}

	@Override
	protected void drawOverlayBitmap(Canvas arg0, Point arg1,
			Projection arg2, byte arg3) {

		this.draw(arg0,null,false);
	}

	public Point getP1() {
		return p1;
	}

	public void setP1(Point p1) {
		this.p1 = p1;
	}

	public Point getP2() {
		return p2;
	}

	public void setP2(Point p2) {
		this.p2 = p2;
	}
}

